package com.leapp.sk.viewdelegate.classs

import android.text.TextUtils
import android.widget.ListView
import android.widget.TextView
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.bean.ChoseClassEntity
import com.leapp.sk.ui.activity.classs.ChoseClassActivity
import com.leapp.sk.ui.activity.classs.ClassAlbumActivity
import com.leapp.sk.ui.adapter.ChoseClassAdapter

/**
 * 选择班级视图托管类
 * @author  chaohaipeng  on 2018/8/2.
 */
class ChoseClassDelegate : BaseAppDelegate() {

    /**
     * 班级选择适配器
     */
    var choseClassAdapter:ChoseClassAdapter?=null
    /**
     * 所选班级名称
     */
    var stringBuilderClassName:StringBuilder?=null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_chose_class
    }


    /**
     * 初始化适配器
     */
    fun initAdapter(data: List<ChoseClassEntity>) {
        val lv_acc_list = getView<ListView>(R.id.lv_acc_list)
      choseClassAdapter = ChoseClassAdapter(getActivity(), data)
        lv_acc_list.adapter = choseClassAdapter
        lv_acc_list.setOnItemClickListener{adapterView, view, i, l ->
            val check = choseClassAdapter?.mList?.get(i)?.isCheck
            if (check!!){
                choseClassAdapter?.mList?.get(i)?.isCheck=false
            }else{
                choseClassAdapter?.mList?.get(i)?.isCheck=true
            }
            choseClassAdapter?.notifyDataSetChanged()
        }
        stringBuilderClassName=StringBuilder()
    }

    /**
     * 班级名称
     */
     fun getClassName():String{
        val length = stringBuilderClassName?.length
        if (length!=null){
            if (length>0){
                stringBuilderClassName?.delete(0,stringBuilderClassName!!.length)
            }
        }
        val indices = choseClassAdapter?.mList?.indices
        if (indices!=null){
            for (i in indices ){
                val check = choseClassAdapter?.mList?.get(i)?.isCheck!!
                if (check){
                    stringBuilderClassName?.append(choseClassAdapter?.mList?.get(i)?.className)?.append("、")
                }
            }
        }
        val lengths = stringBuilderClassName?.length
        if (lengths!=null){
            if (lengths>0){
                stringBuilderClassName?.delete(stringBuilderClassName!!.length-1,stringBuilderClassName!!.length)
            }
        }
       return stringBuilderClassName.toString()

     }


    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_acc= getView<SKTopBar>(R.id.jtb_acc)
        if (!TextUtils.isEmpty(title)){
            jtb_acc.setTitle(title)
        }
        setEvent(jtb_acc)
    }
    /**
     * 设置设置事件
     */
    fun setEvent(topbar: SKTopBar){
        topbar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<ChoseClassActivity>().finish()
            }
            override fun rightOnClick() {

            }

        })
    }

}